package util;

import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectRequest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;

/**
 * @program: sijiacheng-obs-use
 * @create: 2023-06-29
 * 上传对象
 **/
public class OBSUploadFile {

    public static String ak="";
    public static String sk="";
    public static String endPoint="";

    public static void main(String[] args) {
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 start
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("huaweicloud.properties");
            PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
            ak=bundle.getString("ak");
            sk=bundle.getString("sk");
            endPoint=bundle.getString("endPoint");
        } catch (IOException e) {
            System.out.println("从resources内properties配置文件中,获取密钥失败。");
            throw new RuntimeException(e);
        }
        /*// Endpoint请按实际情况填写。
        String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
        String ak = "UEYW*******KSV";
        String sk = "PnXLi************9su5IkhDRB";*/
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 end

        // 创建ObsClient实例
        final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

        // localfile为待上传的本地文件路径，需要指定到具体的文件名
//        obsClient.putObject("sjcobs", "apache-maven-3.8.6-bin.zip", new File("D:\\devtools\\apache-maven-3.8.6-bin.zip"));

        // localfile2 为待上传的本地文件路径，需要指定到具体的文件名
        PutObjectRequest request = new PutObjectRequest();
        //设置桶名称
        //设置桶内文件名
        //设置上传文件位置信息
        request.setBucketName("sjcobs");
        request.setObjectKey("apache-maven-3.8.6-bin2.zip");
        request.setFile(new File("D:\\devtools\\apache-maven-3.8.6-bin.zip"));
        obsClient.putObject(request);

    }
}